package ex 
{
	import flash.geom.Rectangle;
	import flash.geom.Point;
	import ex.Circle;
	
	/**
	 * ...
	 * @author ...
	 */
	public class Rectangle extends flash.geom.Rectangle 
	{
		
		public function Rectangle(x:Number=0, y:Number=0, width:Number=0, height:Number=0) 
		{
			super(x, y, width, height);
		}
		
		public function containsCircle (circle:Circle) : Boolean
		{
			return containsRect(circle.bounds());
		}
		
		public function intersectsCircle (circle:Circle) : Boolean
		{
			var p:Point = new Point;
			return intersectsCircleNormal(circle, p);
		}
		
		public function get topRight () : flash.geom.Point
		{
			return new Point(top, right);
		}
		
		public function get bottomLeft () : flash.geom.Point
		{
			return new Point(bottom, left);
		}
		
		public function intersectsCircleNormal (circle:Circle, norma:Point) : Boolean
		{
			
			var r:flash.geom.Rectangle = clone();
			norma.x = 0;
			norma.y = 0;
			r.inflate(circle.diameter,0);
			if (r.containsPoint(circle.center)) 
			{
				if (circle.center.x < x)
				{
					norma.x = -1;
				}
				if (circle.center.x > x + width)
				{
					norma.x = 1;
				}
				return true;
			}
			r = clone();
			r.inflate(0, circle.diameter);
			if (r.containsPoint(circle.center)) 
			{
				if (circle.center.y < y)
				{
					norma.y = -1;
				}
				if (circle.center.y > y + height)
				{
					norma.y = 1;
				}
				return true;
			}
			r.inflate(circle.diameter, 0);
			if (r.containsPoint(circle.center))
			{
				if (circle.contains(left, top))
				{
					norma = topLeft.subtract(circle.center);
					norma.normalize(1);
					return true;
				}
				else if (circle.contains(left, bottom))
				{
					norma = bottomLeft.subtract(circle.center);
					norma.normalize(1);
					return true;
				}
				else if (circle.contains(right, top))
				{
					norma = topRight.subtract(circle.center);
					norma.normalize(1);
					return true;
				}
				else if (circle.contains(right, bottom))
				{
					norma = bottomRight.subtract(circle.center);
					norma.normalize(1);
					return true;
				}
			}
			return false;
		}
	}

}